package com.echovin.esb.gateway.oauth2.dao.impl;

import com.echovin.esb.gateway.oauth2.dao.PermissionAndRoleRepository;
import com.echovin.esb.gateway.oauth2.model.PermissionAndRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class PermissionAndRoleRepositoryImpl implements PermissionAndRoleRepository {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    private static PermissionAndRoleRowMapper permissionAndRoleRowMapper = new PermissionAndRoleRowMapper();

    public PermissionAndRoleRepositoryImpl() {
    }

    @Override
    public List<PermissionAndRole> getAll() {
        String sql = " select pr.ROLE_ID, p.uri from YKYLOGIC_AUTH_ROLE_PERMISSION pr , YKYLOGIC_AUTH_PERMISSION p where pr.PERMISSION_ID = p.ID ";
        return this.jdbcTemplate.query(sql, new Object[0], permissionAndRoleRowMapper);
    }

    @Override
    public List<PermissionAndRole> getAllInterface() {
        String sql = " select pr.ROLE_ID, p.uri from YKYLOGIC_AUTH_ROLE_PERMISSION pr , YKYLOGIC_AUTH_PERMISSION p where (pr.PERMISSION_ID = p.ID AND p.type='06') OR (pr.PERMISSION_ID = p.ID AND p.type='01')";
        return this.jdbcTemplate.query(sql, new Object[0], permissionAndRoleRowMapper);
    }
}
